<?php
namespace app\api\controller\Content;

use app\api\controller\Common;
use app\api\Logic\CommonLogic;
use app\api\model\ArticleModel;

/**
 * 内容中心
 * 萌宝圈
 */
class Video extends Common{
    protected $noNeedLogin = ['*'];

    /**
     * 发布视频
     */
    public function release(){
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证数据
            $param = $this->contentVerifyData($param);
            if(empty($param['title'])){
                $this->error('未传入标题');
            }
            if(empty($param['video'])){
                $this->error('未传入视频');
            }
            $model = new ArticleModel;
            $model->save([
                'shop_id'=>$param['shop_id'],
                'type'=>4,
                'title'=>$param['title'],
                'video'=>$param['video'],
                'operation_id'=>$param['teacher_id'],
                'createtime'=>time()
            ]);
            return $this->success('发布成功');
        }
        $this->error('请求错误');
    }

    /**
     * 视频列表
     */
    public function list(){
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证数据
            $param = $this->contentVerifyData($param);
            $page = $param['page'];
            $limit = $param['page_size'];
            $teacher_id = $param['teacher_id'];
            $where = [
                'a.shop_id'=>$param['shop_id'],
                'a.operation_id'=>$param['teacher_id'],
                'a.status'=>1,
                'a.type'=>4,
                'a.deletetime' => null
            ];
            $order = 'a.is_top desc,a.createtime desc';
            $list = ArticleModel::alias('a')
                ->where($where)
                ->join('likes c',"a.id = c.relation_id and c.style = 'Article' and c.user_id='{$teacher_id}' ",'left')
                ->group('a.id')
                ->order($order)
                ->field('a.id,a.title,a.video,a.like,a.views,a.createtime,count(distinct c.id) as is_like')
                ->limit(($page-1)*$limit,$limit)
                ->select();
            return $this->success('查询成功',$list);
        }
        $this->error('请求错误');
    }

    /**
     * 视频详情
     */
    public function info(){
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证数据
            $param = $this->contentVerifyData($param);
            $page = $param['page'];
            $where = [
                'a.shop_id'=>$param['shop_id'],
                'a.operation_id'=>$param['teacher_id'],
                'a.status'=>1,
                'a.type'=>4,
                'a.deletetime' => null
            ];
            $teacher_id = $param['teacher_id'];
            $order = 'a.is_top desc,a.createtime desc';
            if(isset($param['id'])){
                $where['a.id'] = $param['id'];
                $list = ArticleModel::alias('a')
                ->where($where)
                ->join('likes c',"a.id = c.relation_id and c.style = 'Article' and c.user_id='{$teacher_id}' ",'left')
                ->group('a.id')
                ->field('a.id,a.title,a.video,a.like,a.views,a.createtime,count(distinct c.id) as is_like')
                ->select();
            }else{
                $list = ArticleModel::alias('a')
                ->where($where)
                ->join('likes c',"a.id = c.relation_id and c.style = 'Article' and c.user_id='{$teacher_id}' ",'left')
                ->group('a.id')
                ->order($order)
                ->field('a.id,a.title,a.video,a.like,a.views,a.createtime,count(distinct c.id) as is_like')
                ->limit($page-1,1)
                ->select();
            }
            return $this->success('查询成功',$list);
        }
        $this->error('请求错误');
    }

     /**
     * 删除
     */
    public function del(){
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证数据
            $param = $this->contentVerifyData($param);
            if(empty($param['id'])){
                $this->error('未传入id');
            }
            $res = ArticleModel::where([
                'id'=>$param['id'],
                'shop_id'=>$param['shop_id'],
                'operation_id'=>$param['teacher_id']
            ])->update(['deletetime'=>time()]);
            if($res){
                return $this->success('删除成功');
            }
            $this->error('删除失败');
        }
        $this->error('请求错误');
    }

    /**
     * 视频点赞
     *
     */
    public function clickLikes()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证数据
            $param = $this->contentVerifyData($param);

            if(empty($param['id'])){
                $this->error('未传入视频id');
            }
            $commonLogic = new CommonLogic();
            $res = $commonLogic->giveLikes($param['teacher_id'],$param['shop_id'],$param['id'],'Article','article','like');
            if($res){
                $this->success('操作成功',['status'=>$res]);
            }
            $this->error('操作失败');
        }
        $this->error('请求错误');
    }
}